from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from django.urls import reverse
from .. import models
from django.utils.html import format_html
from . import goods_views, users_views, index_views, category_views
from django.core.paginator import Paginator
from django.db.models import Q
import os

# 添加
def addgoods(request):
    # 查询数据分类
    types = category_views.tab()
    # 返回
    print('types',types)
    return render(request, 'myadmin/goods/addgoods.html', {'cates': types})


def goodslist(request):
    goods = models.Goods.objects.all()
    print('goods',goods)
    types = request.GET.get('type')
    # 接受关键字
    search = request.GET.get('search')
    # 判断用是否搜索内容
    if types:
        if types == 'all':
            goods = models.Goods.objects.filter(Q(gname__contains=search))
        elif types == 'gname':
            goods = models.Goods.objects.filter(gname__contains=search)

    # 实例化分页对象
    p = Paginator(goods, 10)

    # 一共可以分多少页
    sumpage = p.num_pages

    # 取第几页的数据
    # 接受用户的页码
    page = int(request.GET.get('p', 1))
    # 第几页的数据
    page1 = p.page(page)

    # 判断 如果用输入的页码<=3 显示前五个页码
    if page <= 3:
        # 页码的迭代序列  [1,2,3,4,5,6,7]
        prange = p.page_range[:5]
    elif page + 2 >= sumpage:
        prange = p.page_range[-5:]
    else:
        prange = p.page_range[page - 3:page + 2]


    # 进行数据分配 通过 模板语法将数据展示到模板当中
    print('page1',page1)
    return render(request, 'myadmin/goods/goods.html',{'goods': page1, 'prange': prange, 'page': page, 'sumpage': sumpage})


def goodsinsert(request):
    # 接受用户的信息
    ginfo = request.POST.dict()

    ginfo.pop('csrfmiddlewaretoken')
    print(ginfo)

    file = request.FILES.get('gimgurl')
    if not file:
        return HttpResponse('<script>alert("请选择图片");history.back(-1)</script>')

    # 调图片上传
    gimgurl = users_views.upload(file)

    # 入库
    goods = models.Goods()

    goods.gname = ginfo['title']

    goods.gimgurl = gimgurl

    goods.gprice = ginfo['price']

    goods.gordernum = ginfo['ordernum']


    goods.ginfo = ginfo['ginfo']

    goods.categoryid = models.Category.objects.get(id=ginfo['cateid'])

    goods.save()

    print(goods)
    # 返回
    return HttpResponse(
        "<script>alert('成功添加商品信息!');location.href = '" + reverse('myadmin_addgoods') + "' </script>")



# 修改
def editgoods(request):
    if request.method == "GET":
        id = request.GET.get('id')
        goodsinfo = models.Goods.objects.get(id=id)
        ginfo = format_html(goodsinfo.ginfo)
        content = {'goodsinfo':goodsinfo,'ginfo':ginfo}
        return render(request,'myadmin/goods/editgoods.html',content)
    elif request.method == "POST":
        id = request.GET.get('gid')
        goodsinfo = models.Goods.objects.get(id = id)
        file = request.FILES.get('gimgurl')
        if not file:
            return HttpResponse('<script>alert("请选择图片");history.back(-1)</script>')
        os.remove("." + goodsinfo.gimgurl)
        gimgurl = users_views.upload(file)
        goodsinfo.gname = request.POST['gname']
        goodsinfo.gprice = request.POST['gprice']
        goodsinfo.gimgurl = gimgurl
        goodsinfo.gordernum = request.POST['gordernum']
        goodsinfo.ginfo = request.POST['ginfo']
        goodsinfo.save()
        return redirect(reverse('myadmin_goodslist'))

# 删除
def delgoods(request):
    id = request.GET.get('id')
    goods = models.Goods.objects.get(id=id)
    goods.delete()
    return redirect(reverse('myadmin_goodslist'))
